home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / ka9q / expiry / isittime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-10  |  1.3 KB  |  52 lines

  1. /* Is it time for something to happen? */
  2.  
  3. /* Written by Bernie Roehl, May 1990 */
  4.  
  5. /* First line of file is elapsed time since GMT origin, in seconds;
  6.    second line is human-readable date/time (from ctime()) */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. char *progname = "ISITTIME";
  13.  
  14. void main(int argc, char *argv[])
  15.     {
  16.     long inter;
  17.     FILE *tmpf;
  18.     time_t then, now;
  19.     if (argc < 3 || argc > 4) {
  20.         printf("%s: correct usage is 'ISITTIME file interval [units]'\n", progname);
  21.         exit(2);
  22.         }
  23.     inter = atol(argv[2]);  /* interval (in seconds if no units given) */
  24.     if (argc > 3)  /* units were specified... scale interval */
  25.         switch(tolower(*argv[3])) {
  26.             case 'd': inter *= 24;
  27.             case 'h': inter *= 60;
  28.             case 'm': inter *= 60;
  29.             case 's': break;
  30.             default: printf("%s: unit '%s' bad; must be one of days, hours, minutes or seconds\n", progname, argv[3]);
  31.                     exit(3);
  32.                     break;
  33.             }
  34.     time(&now);
  35.     if ((tmpf = fopen(argv[1], "r")) != NULL) {
  36.         fscanf(tmpf, "%ld", &then);
  37.         fclose(tmpf);
  38.         }
  39.     else
  40.         then = 0L;  /* the beginning of time :-) */
  41.     if ((then + inter) < now) {
  42.         if ((tmpf = fopen(argv[1], "w")) == NULL)
  43.             printf("%s: can't update '%s'\n", progname, argv[1]);
  44.         else {
  45.             fprintf(tmpf, "%ld\n%s", now, ctime(&now));
  46.             fclose(tmpf);
  47.             }
  48.         exit(1);
  49.         }
  50.     exit(0);
  51.     }
  52.